﻿using System;
using System.Data;
using System.Text;
using System.Data.SqlClient;
using CrazyTalk.Data;
using CrazyTalk.IDAL;
using CrazyTalk.Models;

//Please add references
namespace CrazyTalk.MSSQLDAL
{
	/// <summary>
	/// 数据访问类:UserSessionDAL
	/// </summary>
	public class UserSessionDAL:IUserSessionDAL
	{
		public UserSessionDAL()
		{}
		#region  Method

		/// <summary>
		/// 得到最大ID
		/// </summary>
		public int GetMaxId()
		{
		return DbHelperSQL.GetMaxID("RowID", "UserSession"); 
		}

		/// <summary>
		/// 是否存在该记录
		/// </summary>
		public bool Exists(int RowID)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select count(1) from UserSession");
			strSql.Append(" where RowID=@RowID");
			SqlParameter[] parameters = {
					new SqlParameter("@RowID", SqlDbType.Int,4)
			};
			parameters[0].Value = RowID;

			return DbHelperSQL.Exists(strSql.ToString(),parameters);
		}


		/// <summary>
		/// 增加一条数据
		/// </summary>
		public int Add(UserSession model)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("insert into UserSession(");
			strSql.Append("UserToken,UserID,RoleID,IP,ActionType,StampTime,Resource,LastActiveTime,NickName)");
			strSql.Append(" values (");
			strSql.Append("@UserToken,@UserID,@RoleID,@IP,@ActionType,@StampTime,@Resource,@LastActiveTime,@NickName)");
			strSql.Append(";select @@IDENTITY");
			SqlParameter[] parameters = {
					new SqlParameter("@UserToken", SqlDbType.Char,10),
					new SqlParameter("@UserID", SqlDbType.VarChar,20),
					new SqlParameter("@RoleID", SqlDbType.Int,4),
					new SqlParameter("@IP", SqlDbType.VarChar,15),
					new SqlParameter("@ActionType", SqlDbType.Char,1),
					new SqlParameter("@StampTime", SqlDbType.DateTime),
					new SqlParameter("@Resource", SqlDbType.Char,1),
					new SqlParameter("@LastActiveTime", SqlDbType.DateTime),
					new SqlParameter("@NickName", SqlDbType.NVarChar,50)};
			parameters[0].Value = model.UserToken;
			parameters[1].Value = model.UserID;
			parameters[2].Value = model.RoleID;
			parameters[3].Value = model.IP;
			parameters[4].Value = model.ActionType;
			parameters[5].Value = model.StampTime;
			parameters[6].Value = model.Resource;
			parameters[7].Value = model.LastActiveTime;
			parameters[8].Value = model.NickName;

			object obj = DbHelperSQL.GetSingle(strSql.ToString(),parameters);
			if (obj == null)
			{
				return 0;
			}
			else
			{
				return Convert.ToInt32(obj);
			}
		}
		/// <summary>
		/// 更新一条数据
		/// </summary>
		public bool Update(UserSession model)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("update UserSession set ");
			strSql.Append("UserToken=@UserToken,");
			strSql.Append("UserID=@UserID,");
			strSql.Append("RoleID=@RoleID,");
			strSql.Append("IP=@IP,");
			strSql.Append("ActionType=@ActionType,");
			strSql.Append("StampTime=@StampTime,");
			strSql.Append("Resource=@Resource,");
			strSql.Append("LastActiveTime=@LastActiveTime,");
			strSql.Append("NickName=@NickName");
			strSql.Append(" where RowID=@RowID");
			SqlParameter[] parameters = {
					new SqlParameter("@UserToken", SqlDbType.Char,10),
					new SqlParameter("@UserID", SqlDbType.VarChar,20),
					new SqlParameter("@RoleID", SqlDbType.Int,4),
					new SqlParameter("@IP", SqlDbType.VarChar,15),
					new SqlParameter("@ActionType", SqlDbType.Char,1),
					new SqlParameter("@StampTime", SqlDbType.DateTime),
					new SqlParameter("@Resource", SqlDbType.Char,1),
					new SqlParameter("@LastActiveTime", SqlDbType.DateTime),
					new SqlParameter("@NickName", SqlDbType.NVarChar,50),
					new SqlParameter("@RowID", SqlDbType.Int,4)};
			parameters[0].Value = model.UserToken;
			parameters[1].Value = model.UserID;
			parameters[2].Value = model.RoleID;
			parameters[3].Value = model.IP;
			parameters[4].Value = model.ActionType;
			parameters[5].Value = model.StampTime;
			parameters[6].Value = model.Resource;
			parameters[7].Value = model.LastActiveTime;
			parameters[8].Value = model.NickName;
			parameters[9].Value = model.RowID;

			int rows=DbHelperSQL.ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}

		/// <summary>
		/// 删除一条数据
		/// </summary>
		public bool Delete(int RowID)
		{
			
			StringBuilder strSql=new StringBuilder();
			strSql.Append("delete from UserSession ");
			strSql.Append(" where RowID=@RowID");
			SqlParameter[] parameters = {
					new SqlParameter("@RowID", SqlDbType.Int,4)
			};
			parameters[0].Value = RowID;

			int rows=DbHelperSQL.ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}
		/// <summary>
		/// 批量删除数据
		/// </summary>
		public bool DeleteList(string RowIDlist )
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("delete from UserSession ");
			strSql.Append(" where RowID in ("+RowIDlist + ")  ");
			int rows=DbHelperSQL.ExecuteSql(strSql.ToString());
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}


		/// <summary>
        /// 根据UserToken得到一个对象实体
		/// </summary>
        public UserSession GetModel(string userToken)
		{
			
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select  top 1 RowID,UserToken,UserID,RoleID,IP,ActionType,StampTime,Resource,LastActiveTime,NickName from UserSession ");
            strSql.Append(" where UserToken=@UserToken");
			SqlParameter[] parameters = {
					new SqlParameter("@RowID", SqlDbType.NChar)
			};
            parameters[0].Value = userToken;

			UserSession model=new UserSession();
			DataSet ds=DbHelperSQL.Query(strSql.ToString(),parameters);
			if(ds.Tables[0].Rows.Count>0)
			{
				if(ds.Tables[0].Rows[0]["RowID"]!=null && ds.Tables[0].Rows[0]["RowID"].ToString()!="")
				{
					model.RowID=int.Parse(ds.Tables[0].Rows[0]["RowID"].ToString());
				}
				if(ds.Tables[0].Rows[0]["UserToken"]!=null && ds.Tables[0].Rows[0]["UserToken"].ToString()!="")
				{
					model.UserToken=ds.Tables[0].Rows[0]["UserToken"].ToString();
				}
				if(ds.Tables[0].Rows[0]["UserID"]!=null && ds.Tables[0].Rows[0]["UserID"].ToString()!="")
				{
					model.UserID=ds.Tables[0].Rows[0]["UserID"].ToString();
				}
				if(ds.Tables[0].Rows[0]["RoleID"]!=null && ds.Tables[0].Rows[0]["RoleID"].ToString()!="")
				{
					model.RoleID=int.Parse(ds.Tables[0].Rows[0]["RoleID"].ToString());
				}
				if(ds.Tables[0].Rows[0]["IP"]!=null && ds.Tables[0].Rows[0]["IP"].ToString()!="")
				{
					model.IP=ds.Tables[0].Rows[0]["IP"].ToString();
				}
				if(ds.Tables[0].Rows[0]["ActionType"]!=null && ds.Tables[0].Rows[0]["ActionType"].ToString()!="")
				{
					model.ActionType=ds.Tables[0].Rows[0]["ActionType"].ToString();
				}
				if(ds.Tables[0].Rows[0]["StampTime"]!=null && ds.Tables[0].Rows[0]["StampTime"].ToString()!="")
				{
					model.StampTime=DateTime.Parse(ds.Tables[0].Rows[0]["StampTime"].ToString());
				}
				if(ds.Tables[0].Rows[0]["Resource"]!=null && ds.Tables[0].Rows[0]["Resource"].ToString()!="")
				{
					model.Resource=ds.Tables[0].Rows[0]["Resource"].ToString();
				}
				if(ds.Tables[0].Rows[0]["LastActiveTime"]!=null && ds.Tables[0].Rows[0]["LastActiveTime"].ToString()!="")
				{
					model.LastActiveTime=DateTime.Parse(ds.Tables[0].Rows[0]["LastActiveTime"].ToString());
				}
				if(ds.Tables[0].Rows[0]["NickName"]!=null && ds.Tables[0].Rows[0]["NickName"].ToString()!="")
				{
					model.NickName=ds.Tables[0].Rows[0]["NickName"].ToString();
				}
				return model;
			}
			else
			{
				return null;
			}
		}

		/// <summary>
		/// 获得数据列表
		/// </summary>
		public DataSet GetList(string strWhere)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select RowID,UserToken,UserID,RoleID,IP,ActionType,StampTime,Resource,LastActiveTime,NickName ");
			strSql.Append(" FROM UserSession ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			return DbHelperSQL.Query(strSql.ToString());
		}

		/// <summary>
		/// 获得前几行数据
		/// </summary>
		public DataSet GetList(int Top,string strWhere,string filedOrder)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select ");
			if(Top>0)
			{
				strSql.Append(" top "+Top.ToString());
			}
			strSql.Append(" RowID,UserToken,UserID,RoleID,IP,ActionType,StampTime,Resource,LastActiveTime,NickName ");
			strSql.Append(" FROM UserSession ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			strSql.Append(" order by " + filedOrder);
			return DbHelperSQL.Query(strSql.ToString());
		}

		/// <summary>
		/// 获取记录总数
		/// </summary>
		public int GetRecordCount(string strWhere)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select count(1) FROM UserSession ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			object obj = DbHelperSQL.GetSingle(strSql.ToString());
			if (obj == null)
			{
				return 0;
			}
			else
			{
				return Convert.ToInt32(obj);
			}
		}
		/// <summary>
		/// 分页获取数据列表
		/// </summary>
		public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("SELECT * FROM ( ");
			strSql.Append(" SELECT ROW_NUMBER() OVER (");
			if (!string.IsNullOrEmpty(orderby.Trim()))
			{
				strSql.Append("order by T." + orderby );
			}
			else
			{
				strSql.Append("order by T.RowID desc");
			}
			strSql.Append(")AS Row, T.*  from UserSession T ");
			if (!string.IsNullOrEmpty(strWhere.Trim()))
			{
				strSql.Append(" WHERE " + strWhere);
			}
			strSql.Append(" ) TT");
			strSql.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
			return DbHelperSQL.Query(strSql.ToString());
		}

		/*
		/// <summary>
		/// 分页获取数据列表
		/// </summary>
		public DataSet GetList(int PageSize,int PageIndex,string strWhere)
		{
			SqlParameter[] parameters = {
					new SqlParameter("@tblName", SqlDbType.VarChar, 255),
					new SqlParameter("@fldName", SqlDbType.VarChar, 255),
					new SqlParameter("@PageSize", SqlDbType.Int),
					new SqlParameter("@PageIndex", SqlDbType.Int),
					new SqlParameter("@IsReCount", SqlDbType.Bit),
					new SqlParameter("@OrderType", SqlDbType.Bit),
					new SqlParameter("@strWhere", SqlDbType.VarChar,1000),
					};
			parameters[0].Value = "UserSession";
			parameters[1].Value = "RowID";
			parameters[2].Value = PageSize;
			parameters[3].Value = PageIndex;
			parameters[4].Value = 0;
			parameters[5].Value = 0;
			parameters[6].Value = strWhere;	
			return DbHelperSQL.RunProcedure("UP_GetRecordByPage",parameters,"ds");
		}*/

		#endregion  Method
	}
}

